//
//  ViewController3.m
//  SaveData
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController3.h"
#import "StudentModel.h"

@interface ViewController3 ()<UITableViewDataSource,UITableViewDelegate>

// 学生数组
@property (nonatomic,strong) NSMutableArray *stuList;

@end

@implementation ViewController3

/** 重写getter方法 懒加载 */
- (NSMutableArray *)stuList
{
    if (_stuList == nil)
    {
        // 判断存档文件是否存在，如果之前已有存档文件，则从存档中读取出来以前的数据
        NSFileManager *fm = [NSFileManager defaultManager];
        if([fm fileExistsAtPath:[self dataPath]])
        {
            // 解档
            // 1. 读出数据(二进制数据)
            NSData *data = [NSData dataWithContentsOfFile:[self dataPath]];
            // 2. 创建解档对象
            NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            // 3. 从解档对象中解档Student(和归档时使用相同的key)
            _stuList = [unArch decodeObjectForKey:@"Students"];
            // 4. 完成解档
            [unArch finishDecoding];
        }
        if(_stuList == nil)
        {
            _stuList = [NSMutableArray array];
        }
    }
    return _stuList;
}


// 归档文件存储路径
- (NSString *)dataPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"students2.arch"];
    //    NSLog(@"存储位置:%@",filePath);
    return filePath;
}

// 归档
- (void)saveData
{
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [arch encodeObject:self.stuList forKey:@"Students"];
    [arch finishEncoding];
    [mData writeToFile:[self dataPath] atomically:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (IBAction)add:(id)sender
{
    // 1. 将信息存储到对象中
    StudentModel *m = [[StudentModel alloc] initWithId:self.idTextField.text
                                               andName:self.nameTextField.text
                                                andAge:self.ageTextField.text];
    // 2. 将对象加入到数组中
    [self.stuList addObject:m];
    // 3. 保存
    [self saveData];
}
- (IBAction)remove:(id)sender
{
    // 1. 遍历学生数组
    for(StudentModel *m in self.stuList)
    {
        // 2. 如果学号相同
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            // 3. 将该学生这个字典从数组中移除
            [self.stuList removeObject:m];
            break;
        }
    }
    // 4. 数组保存一下
    [self saveData];
}
- (IBAction)modify:(id)sender
{
    // 1. 遍历学生数组
    for(StudentModel *m in self.stuList)
    {
        // 2. 如果学号相同
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            // 3. 修改该学生信息
            m.stuName = self.nameTextField.text;
            m.stuAge = self.ageTextField.text;
            break;
        }
    }
    // 4. 数组保存一下
    [self saveData];
}
- (IBAction)find:(id)sender
{
    // 1. 遍历学生数组
    for(StudentModel *m in self.stuList)
    {
        // 2. 如果学号相同
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            // 3. 将查询到的学生信息显示到界面
            self.nameTextField.text = m.stuName;
            self.ageTextField.text = m.stuAge;
            break;
        }
    }
}
- (IBAction)findAll:(id)sender
{
    for (StudentModel *m in self.stuList)
    {
        NSLog(@"%@",m);
    }
}

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return self.stuList.count;
//}
//
//// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
//// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
//    UILabel *label1 = [cell.contentView viewWithTag:101];
//    UILabel *label2 = [cell.contentView viewWithTag:102];
//    UILabel *label3 = [cell.contentView viewWithTag:103];
//    NSDictionary *dict = self.stuList[indexPath.row];
//    label1.text = dict[@"stuId"];
//    label2.text = dict[@"stuName"];
//    label3.text = dict[@"stuAge"];
//    NSLog(@"%@ %@ %@",label1.text,label2.text,label3.text);
//    
//    return cell;
//}
@end
